//
//  PopoverManager.swift
//
//  Created by jyh on 2017/11/30.
//  Copyright © 2017年 jyh. All rights reserved.
//

import UIKit


class PopoverManager: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    
    private var isPresent = false
    
    /// 保存菜单的尺寸
    var presentFrame = CGRect.zero
    
    // MARK: -- UIViewControllerTransitioningDelegate
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentedView(presentedViewController: presented, presenting: presenting)
        pc.presentFrame =  presentFrame
        return pc
        
    }
    
    // 动画出现
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//        NotificationCenter.default.post(name: NSNotification.Name(rawValue: PopoverPresentationManagerDidPresented), object: self)
        isPresent = true
        return self
    }
    
    // 动画消失
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//        NotificationCenter.default.post(name: NSNotification.Name(rawValue: PopoverPresentationManagerDidDismissed), object: self)
        isPresent = false
        return self
    }
    
    // MARK: -- UIViewControllerAnimatedTransitioning
    // 展现和消失时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    // 管理modal展现和消失,展现和消失都会调用该方法.系统的所有动画都会被删除.所有动画需要自己添加
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent {
            // 执行展现动画
            willPresentedController(transitionContext: transitionContext)
        } else {
            // 执行消失动画
            willDismissedController(transitionContext: transitionContext)
        }
    }
    
    /// 执行展现动画
    private func willPresentedController(transitionContext: UIViewControllerContextTransitioning) {
        
        //let to = transitionContext.viewController(forKey: .to)
        //let from = transitionContext.viewController(forKey: .from)
        guard let toView = transitionContext.view(forKey: .to) else {
            return
        }
        //        let fromView = transitionContext.view(forKey: .from)
        
        transitionContext.containerView.addSubview(toView)
        
//        toView.transform = CGAffineTransform(scaleX: 1.0,y: 0.5)
        // 修改锚点,可以改变动画出现的位置
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.1)
        
        toView.alpha = 0
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            // 清空形变
            //toView.transform = CGAffineTransform.identity
            toView.alpha = 0.8
        }) { (_) in
            // 结束动画,必须执行
            transitionContext.completeTransition(true)
        }
    }
    
    /// 执行消失动画
    private func willDismissedController(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let formView = transitionContext.view(forKey: .from) else {
            return
        }
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            formView.alpha = 0
            //formView.transform = CGAffineTransform(scaleX: 1.0,y: 0.00001)
        }, completion: { (_) in
            // 结束动画,必须执行
            transitionContext.completeTransition(true)
        })
    }
}
